package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.dto.BrandDTO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.BrandServiceRepository;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 程旭东
 */
@Slf4j
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private BrandServiceRepository brandServiceRepository;

    @Override
    public void add(BrandListItemVO brandListItemVO) {
        String name = brandListItemVO.getName();
        BrandListItemVO brandListItemVO1 = brandMapper.selectByName(name);

        if(brandListItemVO1!=null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"添加品牌失败，已存在同名品牌");
        }

        brandMapper.insert(brandListItemVO);
    }

    @Override
    public void addNew(BrandDTO brandDTO) {
        String name = brandDTO.getName();
        BrandListItemVO brandListItemVO1 = brandMapper.selectByName(name);

        if(brandListItemVO1!=null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"添加品牌失败，已存在同名品牌");
        }

        brandMapper.insertNew(brandDTO);
    }

    @Override
    public void deleteById(Long id) {
        BrandListItemVO brandListItemVO = brandMapper.selectById(id);
        if (brandListItemVO == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"删除品牌失败，数据库中不存在该品牌");
        }

        brandMapper.deleteById(id);
    }

    @Override
    public List<BrandListItemVO> list() {
        List<BrandStandardVO> list = brandMapper.list();
        List<BrandListItemVO> brandListItemVOList = new ArrayList<>();
        Iterator<BrandStandardVO> iterator = list.iterator();
        while(iterator.hasNext()){
            BrandListItemVO brandListItemVO = new BrandListItemVO();
            BrandStandardVO brandStandardVO = iterator.next();
            BeanUtils.copyProperties(brandStandardVO,brandListItemVO);
            brandListItemVOList.add(brandListItemVO);
        }
        return brandListItemVOList;
    }

    @Override
    public BrandStandardVO getById(Long id) {
        BrandStandardVO brand = brandMapper.getStandardById(id);
        if(brand == null){
            String message = "获取品牌数据失败，未查询到该数据信息";
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        return brand;
    }

    @Override
    public void rebuildCache() {
        log.debug("开始处理缓存预热");

        log.debug("从MySQL中读取品牌列表");
        List<BrandListItemVO> list = this.list();

        log.debug("将品牌列表写入Redis中");
        brandServiceRepository.save(list);

        log.debug("逐一从mysql中读取出品牌详情，然后写入到Redis中");
        for(BrandListItemVO brandListItemVO : list){
            BrandStandardVO brandStandardVO = this.getById(brandListItemVO.getId());
            brandServiceRepository.save(brandStandardVO);
        }

        log.debug("将Redis中品牌数据进行删除");
        brandServiceRepository.deleteAll();
    }
}
